package main

func getMaxBit(v int) int {
	i := 0
	for v > 0 {
		v >>= 1
		i++
	}
	return i
}

func maximumOr(nums []int, k int) int64 {
	m := map[int][]int{} // 位数 -> nums
	max := 0
	for _, v := range nums {
		bits := getMaxBit(v)
		m[bits] = append(m[bits], v)
		if bits > max {
			max = bits
		}
	}
	presum := 0
	for bits, vals := range m {
		if bits < max {
			for _, v := range vals {
				presum |= v
			}
		}
	}
	var maxRes int64
	for i, v := range m[max] {
		res := int64(v) << k
		res |= int64(presum)
		for j, v2 := range m[max] {
			if i != j {
				res |= int64(v2)
			}
		}
		if res > maxRes {
			maxRes = res
		}
	}
	return maxRes
}

func main() {
	res := maximumOr([]int{8, 9, 1, 2}, 2)
	println(res)
}
